﻿#region Using Directives

    using System;
    using System.Text;
    
    using Csla;
    
    using MarketPulse.BusinessLogic.Core;
    using MarketPulse.BusinessLogic.Core.Security;
    using MarketPulse.BusinessLogic.Common;

#endregion

namespace MarketPulse.BusinessLogic.Private
{
    /// <summary>
    /// This class represents a user of the system.  A user could be a mystery shopper, a
    /// contact at a client company, and internal operations user or an IT user. 
    /// </summary>
    [Serializable]
    [Csla.Server.ObjectFactory("MarketPulse.DataAccess.Factory.UserFactory, MarketPulse.DataAccess.Factory", "FetchInfoList")]
    public class User : MarketPulseBase<User>
    {

        #region Constructors

            /// <summary>
            /// default constructor is private in order to (try to) enforce construction
            /// only via the static factory methods provided.
            /// </summary>
            private User()
            {
            }

        #endregion

        #region Methods

            #region Factory Methods

                /// <summary>
                /// Method that creates a new User via the DataPortal client proxy
                /// </summary>
                /// <returns>The newly created User object</returns>
                public static User New()
                {
                    return DataPortal.Create<User>();
                }
        
                /// <summary>
                /// Method that gets the specified User via the DataPortal client proxy 
                /// </summary>
                /// <param name="guid">The user Guid to retrieve</param>
                /// <returns>The populated user object</returns>
                public static User Get(Guid guid)
                {
                    return DataPortal.Fetch<User>(new Csla.SingleCriteria<Guid>(guid));
                }

                /// <summary>
                /// Method that gets the specified User via the DataPortal client proxy 
                /// </summary>
                /// <param name="userName">The user Name to retrieve</param>
                /// <returns>The populated user object</returns>
                public static User Get(string userName)
                {
                    return DataPortal.Fetch<User>(new Csla.SingleCriteria<string>(userName));
                }

                ///// <summary>
                ///// Method that gets the specified User via the DataPortal client proxy 
                ///// </summary>
                ///// <param name="userName">The user Name to retrieve</param>
                ///// <returns>The populated user object</returns>
                //public static UserInfoList Get(Guid guid)
                //{
                //    return DataPortal.FetchList<User>(new Csla.SingleCriteria<string>(userName));
                //}   

            #endregion

            /// <summary>
            /// (Overridden) method used to provide a string based representation of the object. In this case the full name of the user
            /// </summary>
            /// <returns></returns>
            public override string ToString()
            {
                StringBuilder fullName = new StringBuilder();
                fullName.Append(Firstname);
                fullName.Append(" ");
                fullName.Append(Lastname);
                return fullName.ToString();
            }

        #endregion

        #region Properties

            /// <summary>
            /// The username for the user
            /// </summary>
            public static PropertyInfo<string> UsernameProperty = RegisterProperty<string>(p => p.Username);
            /// <summary>
            /// The username for the user
            /// </summary>
            public string Username
            {
                get { return GetProperty(UsernameProperty); }
                set { SetProperty(UsernameProperty, value); }
            }

            /// <summary>
            /// The password for the user
            /// </summary>
            public static PropertyInfo<string> PasswordProperty = RegisterProperty<string>(p => p.Password);
            /// <summary>
            /// The password for the user
            /// </summary>
            public string Password
            {
                get { return GetProperty(PasswordProperty); }
                private set { SetProperty(PasswordProperty, value); }
            }


            /// <summary>
            /// The first name for this user
            /// </summary>
            public static PropertyInfo<string> FirstnameProperty = RegisterProperty<string>(p => p.Firstname);
            /// <summary>
            /// The first name for this user
            /// </summary>
            public string Firstname
            {
                get { return GetProperty(FirstnameProperty); }
                set { SetProperty(FirstnameProperty, value); }
            }

            /// <summary>
            /// The last name for this user
            /// </summary>
            public static PropertyInfo<string> LastNameProperty = RegisterProperty<string>(p => p.Lastname);
            /// <summary>
            /// The last name for this user
            /// </summary>
            public string Lastname
            {
                get { return GetProperty(LastNameProperty); }
                set { SetProperty(LastNameProperty, value); }
            }

            /// <summary>
            /// The email address for the user
            /// </summary>
            public static PropertyInfo<string> EMailProperty = RegisterProperty<string>(p => p.EMail);
            /// <summary>
            /// The email address for the user
            /// </summary>
            public string EMail
            {
                get { return GetProperty(EMailProperty); }
                set { SetProperty(EMailProperty, value); }
            }

            /// <summary>
            /// A numeric value representing the number of failed login attempts for a user
            /// </summary>
            public static PropertyInfo<int> FailedLogonAttemptsProperty = RegisterProperty<int>(p => p.FailedLogonAttempts);
            /// <summary>
            /// A numeric value representing the number of failed login attempts for a user
            /// </summary>
            public int FailedLogonAttempts
            {
                get { return GetProperty(FailedLogonAttemptsProperty); }
                set { SetProperty(FailedLogonAttemptsProperty, value); }
            }

            #region Association Properties (Related Objects)

                /// <summary>
                /// A list of the users visits
                /// </summary>
                public static PropertyInfo<VisitInfoList> mVisitInfoListProperty = RegisterProperty<VisitInfoList>(p => p.VisitInfoList);
                /// <summary>
                /// A list of the users visits
                /// </summary>
                public VisitInfoList VisitInfoList
                {
                    get
                    {
                        if (!FieldManager.FieldExists(mVisitInfoListProperty))
                        {
                            LoadProperty(mVisitInfoListProperty, VisitInfoList.GetByUserGUID(this.GUID.Value));
                            OnPropertyChanged(mVisitInfoListProperty);
                        }
                        return ReadProperty(mVisitInfoListProperty);
                    }
                }

                ///// <summary>
                ///// A list of the users profiles
                ///// </summary>
                //public static PropertyInfo<ProfileList> ProfileListProperty = RegisterProperty<ProfileList>(p => p.ProfileList);
                ///// <summary>
                ///// A list of the users profiles
                ///// </summary>
                //public ProfileList ProfileList
                //{
                //    get
                //    {
                //        if (!FieldManager.FieldExists(ProfileListProperty))
                //        {
                //            LoadProperty(ProfileListProperty, ProfileList.GetByUserGUID(this.GUID.Value));
                //            OnPropertyChanged(ProfileListProperty);
                //        }
                //        return ReadProperty(ProfileListProperty);
                //    }
                //}

            #endregion

        #endregion       

    }
}
